package business;

import cn.hutool.http.HttpUtil;
import net.sf.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class WxHelper {
    public static String appid = "wx5049a8566fb0b957";        // 微信应用id
    public static String appSecret = "8b64e6232115913be47a739634430c09";    //(应用密钥)
    /***
     * 获取acess_token
     *
     * @return
     */
    public static String getAccessToken(){
        String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";
        String backData=HttpUtil.get(url);
        String accessToken = (String) JSONObject.fromObject(backData).get("access_token");
        return accessToken;
    }
    public static String getJSApiTicket(String acess_token){
        String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi";
        String backData=HttpUtil.get(urlStr);
        String ticket = (String) JSONObject.fromObject(backData).get("ticket");
        return  ticket;

    }

    /**
     * 获取签名
     * @param decript
     * @return
     */
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
